#include <REGX52.H>
#include "LCD1602.h"
#include "DS1302.h"
#include "Delay.h"
#include "Key.h"
#include "Timer0.h"
//学习两个思想
//1：按键控制模式。根据不同模式执行不同的操作
//2：学习使用一个变量代表不同含义，如TimeSetSelect代表年月日时分秒
//KeyNumber：按键选择判断
//1：模式选择
//2：选择年月日时分秒
//3：调整时间增加
//4：调整时间减少
//MODE：工作模式
//0：实时显示时间
//1：设置时间
//TimeSetSelect：0~5代表年月日时分秒
//TimeSetFlashFlag：闪烁标志位
unsigned char KeyNumber,MODE,TimeSetSelect,TimeSetFlashFlag;

void TimeShow(void)
{
	DS1302_ReadTime();
	LCD_ShowNum(1,1,DS1302_Time[0],2);
	LCD_ShowNum(1,4,DS1302_Time[1],2);
	LCD_ShowNum(1,7,DS1302_Time[2],2);
	LCD_ShowNum(2,1,DS1302_Time[3],2);
	LCD_ShowNum(2,4,DS1302_Time[4],2);
	LCD_ShowNum(2,7,DS1302_Time[5],2);
}

void TimeSet(void)
{
	if (KeyNumber == 2)//选择年月日时分秒
	{
		TimeSetSelect++;
		TimeSetSelect %= 6;//如果TimeSetSelect>5，清0
	}
	if (KeyNumber == 3)//时间增加
	{
		DS1302_Time[TimeSetSelect]++;
		if (DS1302_Time[0] > 99){DS1302_Time[0] = 0;}//年
		if (DS1302_Time[1] > 12){DS1302_Time[1] = 1;}//月
		if (DS1302_Time[1] == 1 || DS1302_Time[1] == 3 || DS1302_Time[1] == 5 ||
			DS1302_Time[1] == 7 || DS1302_Time[1] == 8 || DS1302_Time[1] == 10 ||
			DS1302_Time[1] == 12)
		{
			if (DS1302_Time[2] > 31){DS1302_Time[2] = 1;}
		}else if (DS1302_Time[1] == 4 || DS1302_Time[1] == 6 || DS1302_Time[1] == 9 ||
				  DS1302_Time[1] == 11)
		{
			if (DS1302_Time[2] > 30){DS1302_Time[2] = 1;}
		}else if (DS1302_Time[1] == 2)
		{
			if ((DS1302_Time[0] % 4 == 0 && DS1302_Time[0] % 100 != 0) || 
				DS1302_Time[0] % 400 == 0)
			{
				if (DS1302_Time[2] > 29){DS1302_Time[2] = 1;}
			}else{
				if (DS1302_Time[2] > 28){DS1302_Time[2] = 1;}
			}
		}
		if (DS1302_Time[3] > 23){DS1302_Time[3] = 0;}//时
		if (DS1302_Time[4] > 59){DS1302_Time[4] = 0;}//分
		if (DS1302_Time[5] > 59){DS1302_Time[5] = 0;}//秒
	}
	if (KeyNumber == 4)//时间减少
	{
		DS1302_Time[TimeSetSelect]--;
		if (DS1302_Time[0] < 0){DS1302_Time[0] = 99;}//年
		if (DS1302_Time[1] < 1){DS1302_Time[1] = 12;}//月
		if (DS1302_Time[1] == 1 || DS1302_Time[1] == 3 || DS1302_Time[1] == 5 ||
			DS1302_Time[1] == 7 || DS1302_Time[1] == 8 || DS1302_Time[1] == 10 ||
			DS1302_Time[1] == 12)
		{
			if (DS1302_Time[2] < 1){DS1302_Time[2] = 31;}
			if (DS1302_Time[2] > 31){DS1302_Time[2] = 1;}
		}else if (DS1302_Time[1] == 4 || DS1302_Time[1] == 6 || DS1302_Time[1] == 9 ||
				  DS1302_Time[1] == 11)
		{
			if (DS1302_Time[2] < 1){DS1302_Time[2] = 30;}
			if (DS1302_Time[2] > 30){DS1302_Time[2] = 1;}
		}else if (DS1302_Time[1] == 2)
		{
			if ((DS1302_Time[0] % 4 == 0 && DS1302_Time[0] % 100 != 0) || 
				DS1302_Time[0] % 400 == 0)
			{
				if (DS1302_Time[2] < 1){DS1302_Time[2] = 29;}
				if (DS1302_Time[2] > 29){DS1302_Time[2] = 1;}
			}else{
				if (DS1302_Time[2] < 1){DS1302_Time[2] = 28;}
				if (DS1302_Time[2] > 28){DS1302_Time[2] = 1;}
			}
		}
		if (DS1302_Time[3] < 0){DS1302_Time[3] = 23;}//时
		if (DS1302_Time[4] < 0){DS1302_Time[4] = 59;}//分
		if (DS1302_Time[5] < 0){DS1302_Time[5] = 59;}//秒
	}
	if (TimeSetSelect == 0 && TimeSetFlashFlag == 1){LCD_ShowString(1,1,"  ");}
	else{LCD_ShowNum(1,1,DS1302_Time[0],2);}
	if (TimeSetSelect == 1 && TimeSetFlashFlag == 1){LCD_ShowString(1,4,"  ");}
	else{LCD_ShowNum(1,4,DS1302_Time[1],2);}
	if (TimeSetSelect == 2 && TimeSetFlashFlag == 1){LCD_ShowString(1,7,"  ");}
	else{LCD_ShowNum(1,7,DS1302_Time[2],2);}
	if (TimeSetSelect == 3 && TimeSetFlashFlag == 1){LCD_ShowString(2,1,"  ");}
	else{LCD_ShowNum(2,1,DS1302_Time[3],2);}
	if (TimeSetSelect == 4 && TimeSetFlashFlag == 1){LCD_ShowString(2,4,"  ");}
	else{LCD_ShowNum(2,4,DS1302_Time[4],2);}
	if (TimeSetSelect == 5 && TimeSetFlashFlag == 1){LCD_ShowString(2,7,"  ");}
	else{LCD_ShowNum(2,7,DS1302_Time[5],2);}
}

void main(void)
{
	LCD_Init();
	DS1302_Init();
	Timer0_Init();
	DS1302_SetTime();
	LCD_ShowString(1,1,"  -  -");
	LCD_ShowString(2,1,"  :  :");
	while (1)
	{
		KeyNumber = Key();
		if (KeyNumber == 1)
		{
			if (MODE==0){MODE = 1;TimeSetSelect = 0;}
			else{MODE = 0;DS1302_SetTime();}
		}
		switch (MODE)
		{
			case 0:TimeShow();break;
			case 1:TimeSet();break;
		}
	}
}

void Timer0_Routine(void) interrupt 1
{
	static unsigned int T0Count;
	//TH0寄存器
	TH0 = 0XFC;
	//TL0寄存器
	TL0 = 0X17 + 1;
	T0Count++;
	if (T0Count >= 500)
	{
		T0Count = 0;
		TimeSetFlashFlag = !TimeSetFlashFlag;
	}
}
	